package com.company.cloud.coupon.modules.app.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.coupon.constant.CouponConstant.CouponObjType;
import com.company.cloud.coupon.modules.app.controller.req.CouponFissionDrawByActParam;
import com.company.cloud.coupon.modules.app.controller.req.CouponFissionDrawByShareParam;
import com.company.cloud.coupon.modules.app.controller.req.CouponFissionMyQuery4PageParam;
import com.company.cloud.coupon.modules.app.controller.req.CouponFissionShareParam;
import com.company.cloud.coupon.modules.app.service.CouponFissionService;
import com.company.cloud.coupon.modules.base.entity.CouponFissionActInfo;
import com.company.cloud.coupon.modules.base.entity.CouponFissionInfo;
import com.company.cloud.coupon.modules.base.entity.CouponInfo;
import com.company.cloud.coupon.modules.base.service.CouponFissionActInfoService;
import com.company.cloud.coupon.modules.base.service.CouponFissionInfoService;
import com.company.cloud.coupon.modules.base.service.CouponInfoService;
import com.company.cloud.coupon.modules.cache.models.CouponFissionModel;
import com.company.cloud.coupon.modules.cache.utils.CouponChangeUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 裂变优惠券
 */
@Login
@RestController
@RequestMapping("/app/op/couponFission")
@Api(tags="OP_App_裂变优惠券")
public class CouponFissionController {

	@Autowired
	private CouponFissionService couponFissionService;
	@Autowired
	private CouponFissionActInfoService couponFissionActInfoService;
	@Autowired
	private CouponFissionInfoService couponFissionInfoService;
	@Autowired
	private CouponInfoService couponInfoService;
	@Autowired
	private UserRemoteService userRemoteService;
	
	
	@PostMapping("/queryMy")
	@ApiOperation(value="查询我的裂变优惠券", response=CouponFissionModel.class)
	public BaseResult queryMy(@RequestBody CouponFissionMyQuery4PageParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<CouponFissionInfo> page = couponFissionInfoService.page(
				new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<CouponFissionInfo>()
				.eq("have_user_id", userInfo.getId())
				.orderByDesc("cdt")
		);
		
		IPage<CouponFissionModel> data=page.convert(new Function<CouponFissionInfo, CouponFissionModel>() {
			@Override
			public CouponFissionModel apply(CouponFissionInfo entity) {
				return CouponChangeUtils.getCouponFission(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/get")
	@ApiOperation(value="获取", response=CouponFissionModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		CouponFissionInfo entity= couponFissionInfoService.getById(id);
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		CouponFissionActInfo couponFissionAct=couponFissionActInfoService.getById(entity.getCouponFissionActId());
		
		List<CouponInfo> couponList=couponInfoService.list(new QueryWrapper<CouponInfo>()
				.eq("source_biz_type", CouponObjType.CouponFission_ObjType)
				.eq("source_biz_id", entity.getId())
				.orderByDesc("have_date")
				);
		Set<Integer> userIds=new HashSet<Integer>();
		if(!CollectionUtils.isEmpty(couponList)) {
			couponList.forEach(item->{
				userIds.add(item.getHaveUserId());
			});
		}
		BaseClientResult<Map<Integer, UserSimple>> userResp=userRemoteService.getUserMapByIds(userIds);
		Map<Integer, UserSimple> userMap=BaseClientResult.getResultData(userResp);
		CouponFissionModel data= CouponChangeUtils.getCouponFission(entity,couponFissionAct,couponList,userMap);
		return BaseResult.ok(data);
	}
	

	@PostMapping("/drawByAct")
	@ApiOperation(value="从活动领取", response=CouponFissionModel.class)
	public BaseResult drawByAct(@RequestBody CouponFissionDrawByActParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		couponFissionService.drawCouponFissionByAct(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/share")
	@ApiOperation(value="分享", response=CouponFissionModel.class)
	public BaseResult share(@RequestBody CouponFissionShareParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		couponFissionService.share(params, userInfo);
		return BaseResult.ok();
	}
	
	
	@PostMapping("/drawByShare")
	@ApiOperation(value="从分享领取", response=BaseResult.class)
	public BaseResult drawByShare(@RequestBody CouponFissionDrawByShareParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		couponFissionService.drawCouponByShare(params, userInfo);
		return BaseResult.ok();
	}
	
	
	
	
	
	
	
	
	
	
	
}
